package weixinkeji.dev.power.center.h;

import java.util.ArrayList;
import java.util.List;
import sys.wx.c.ATR;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.ann.power.head.GL;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.m.db.dao.mysql.Dao2;
import sys.wx.util.ToolService;
import weixinkeji.dev.power.center.bean.SysRP;
import weixinkeji.dev.power.center.check.CheckSysPower;
import weixinkeji.dev.power.center.so.URPSO;

/**
 *
 * @author IK
 */
@H("weixinkeji/dev/power/center/sysrp/adu")
@GL
public class SysRPADU {

    ATR a;                  //ATR是必须要的。它封装了web请求的相关信息
    URPSO so = new URPSO();
    private final CheckSysPower check = new CheckSysPower();

    public SysRPADU(ATR obj) {     //这个有参构造函数，也是必须的。它进行附值
        this.a = obj;
    }

    @M("/add")//weixinkeji/dev/power/center/sysup/adu/add.imy
    public String a1() {

        String rID = a.request.getParameter("rID");
        String pIDs = a.request.getParameter("pID");
        if (isError(pIDs)) {
            return null;
        }
        return ToolService.IsNullOREmpty(pIDs) ? clear(rID) : add(rID, pIDs);
    }

    private String clear(String id) {
        a.out_OK_NO(Dao.executeUpdate(so.clearRolePower(id)));
        return null;
    }

    private String add(String rID, String pIDs) {
        List<SysRP> lup = new ArrayList<>();
        SysRP obj;
        for (String pid : pIDs.split(",")) {
            obj = new SysRP();
            obj.setRole_id(rID);
            obj.setPower_id(pid.trim());
            lup.add(obj);
        }
        a.out_OK_NO(Dao2.sql(so.clearRolePower(rID), so.setRP(lup)));
        return null;
    }

    private boolean isError(String pids) {
        for (String pid : pids.split(",")) {
            if (check.isSysPower(pid.trim())) {
                return true;
            }
        }
        return false;
    }
}
